/**
 * 21/02/2014
 * 
 * IssuetypeController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.IssuetypeBOInterface;
import org.cuatrovientos.erp.model.Issuetype;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller class for Issuetype management
 * @author Javier Tejedor
 */
@Controller
public class IssuetypeController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(IssuetypeController.class);

	@Autowired
	private IssuetypeBOInterface issuetypeBO;

	/**
	 * default constructor
	 */
	public IssuetypeController () {
		logger.info("Issuetype BEAN instantiated");
	}

	/**
	 * handles default /issuetype or /issuetype/list request
	 * @return
	 */
	@RequestMapping({"/issuetype","/issuetype/list"})
	public ModelAndView issuetypeDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Issuetype> issuetypeList = issuetypeBO.selectAll();
		model.put("issuetypes", issuetypeList);
		
		return new ModelAndView("IssuetypeList", model);     
	}


	/**
	 * handles default /issuetype/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/issuetype/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true) 
			String description) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Issuetype> issuetypeList = issuetypeBO.search(description);
		model.put("issuetypes", issuetypeList);
		
		return new ModelAndView("IssuetypeList", model);     
	}

	/**
	 * handles default /issuetypes/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/issuetype/delete"})
	public String deleteIssuetype(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("delete " + id);

		issuetypeBO.deleteById(id);
		
		return "redirect:/issuetype";
	}

	/**
	 * handles default /issuetype/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/issuetype/new"})
	public ModelAndView newIssuetype() {
		logger.info("new issuetype");
		
		Issuetype issuetype = new Issuetype();

		return new ModelAndView("NewIssuetype", "issuetype", issuetype);
	}

	/**
	 * handles default /issuetype/save request
	 * @param issuetype
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/issuetype/save"})
	public String newIssuetype(@Valid Issuetype issuetype, BindingResult bindingResult) {
		logger.info("new issuetype " + issuetype.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("issuetype", issuetype);
			
			return "NewIssuetype";
		}

		issuetypeBO.insert(issuetype);
		
		return "redirect:/issuetype";
	}

	/**
	 * handles default /issuetype/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/issuetype/detail"})
	public ModelAndView issuetypeDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Issuetype issuetype = issuetypeBO.selectById(id);
		model.put("issuetype", issuetype);

		return new ModelAndView("IssuetypeDetail", model);
	}


	/**
	 * handles default /issuetype/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/issuetype/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Issuetype issuetype = issuetypeBO.selectById(id);
		model.put("issuetype", issuetype);
		
		return new ModelAndView("UpdateIssuetype",model);
	}


	/**
	 * handles default /issuetype/saveupdate request
	 * @param issuetype
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/issuetype/saveupdate"})
	public String saveUpdate(@Valid Issuetype issuetype, BindingResult bindingResult) {
		logger.info("update issuetype " + issuetype.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("issuetype", issuetype);
			
			return "UpdateIssuetype";
		}

		issuetypeBO.update(issuetype);
		
		return "redirect:/issuetype";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/issuetype/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexIssuetype";
	}
	
}
